﻿Imports LRS.BLL
Imports System.Windows.Forms

Public Class ManageSupplier
    Private oldRowIndex As Integer = -1
    Private IsAdding As Boolean = False
    Private Sub ManageSupplier_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gridSupplier.AutoGenerateColumns = False
        gridSupplier.DataSource = bSupplier.GetSuppliers()
    End Sub

    Private Sub gridSupplier_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridSupplier.RowEnter
        ucSupplier.Enabled = True
        If (oldRowIndex = e.RowIndex) Then
            Return
        End If
        oldRowIndex = e.RowIndex
        Dim supplier As eSupplier
        supplier = bSupplier.GetSupplierByID(gridSupplier.Rows(e.RowIndex).Cells("SupplierID").Value)
        ucSupplier.SetSupplier(supplier)
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        ucSupplier.ClearText()
        btnCancel.Enabled = True
        btnDelete.Enabled = False
        btnAdd.Enabled = False
        IsAdding = True
        gridSupplier.Enabled = False
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (Not ucSupplier.ucValidateSupp()) Then
            Return
        End If
        Dim supplier As eSupplier = ucSupplier.GetSupplier()
        If (supplier Is Nothing) Then
            Return
        End If
        If (IsAdding) Then
            bSupplier.Insert(supplier)
        Else
            bSupplier.Update(supplier)
        End If
        gridSupplier.Enabled = True
        gridSupplier.DataSource = Nothing
        gridSupplier.DataSource = bSupplier.GetSuppliers()
        btnAdd.Enabled = True
        btnDelete.Enabled = True
        btnCancel.Enabled = False
        IsAdding = False
        Dim i = oldRowIndex
        oldRowIndex = -1
        gridSupplier.Rows(i).Cells("SupplierNumber").Selected = True
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        gridSupplier.Enabled = True
        btnAdd.Enabled = True
        btnDelete.Enabled = True
        IsAdding = False
        btnCancel.Enabled = False
        ucSupplier.Enabled = False
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If (gridSupplier.SelectedCells.Count = 0) Then
            MessageBox.Show("Vui lòng chọn dòng cần xóa")
            Return
        End If
        Dim row As Integer = gridSupplier.SelectedCells(0).RowIndex
        Dim result As DialogResult
        result = MessageBox.Show("Bạn có chắc chắn muốn xóa nhà cung cấp có Số hiệu: " & gridSupplier.Rows(row).Cells("SupplierNumber").Value, "Xác nhận", MessageBoxButtons.OKCancel)
        If (result = DialogResult.OK) Then
            Dim supplierID As Integer = gridSupplier.Rows(row).Cells("ProductID").Value
            bSupplier.DeleteSupplierByID(supplierID)
            gridSupplier.DataSource = Nothing
            gridSupplier.DataSource = bSupplier.GetSuppliers()
            oldRowIndex = -1
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class